﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AJAX_learning.Chapter9
{
	public partial class Chapter9b : System.Web.UI.Page
	{
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			if (!IsPostBack)
			{

				var con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
				var listCmd =
					new SqlCommand("SELECT EmployeeID, TitleOfCourtesy + ' ' + FirstName + ' ' + LastName As FullName FROM Employees",
								   con);
				var da = new SqlDataAdapter(listCmd);
				var ds = new DataSet();
				da.Fill(ds, "Employees");
				EmployeesListBox.DataSource = ds.Tables["Employees"];

				var gridCmd = new SqlCommand("SELECT EmployeeID AS id, FirstName, LastName, Title, City FROM Employees", con);
				var daGrid = new SqlDataAdapter(gridCmd);
				daGrid.Fill(ds, "Employees2");
				
				grid.DataSource = ds.Tables["Employees2"];
				
				this.DataBind();
			}

		}

		protected void GetSelectionOnClick(object sender, EventArgs e)
		{
			Result.Text = "";
			foreach (ListItem item in EmployeesListBox.Items)
			{
				if (item.Selected)
				{
					Result.Text += "- Item selected in EmployeesListbox: " + item.Text + " - " + item.Value + "<br />";
				}
			}
		}
	}
}